﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using BLL;

namespace Client.ViewModel
{
    class HighscoreViewModel : BaseViewModel
    {
        #region Bound properties

        private ObservableCollection<Highscore> _highscoreList = new ObservableCollection<Highscore>();
        public ObservableCollection<Highscore> HighscoreList
        {
            get { return _highscoreList; }
            set { _highscoreList = value; }
        }
        
        #endregion

        public HighscoreViewModel()
        {
            BackgroundWorker _worker = new BackgroundWorker();
            _worker.DoWork += GetHighscores;
            _worker.RunWorkerAsync();
        }

        void GetHighscores(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (var srv = new QuizService.QuizServiceClient())
                {
                    srv.GetHighscoresCompleted += GetHighscoresCompleted;
                    srv.GetHighscoresAsync();
                }
            }
            catch
            {
                MessageBox.Show("Hát ez nem sikerült, bocsi.");
            }
        }

        void GetHighscoresCompleted(object sender, QuizService.GetHighscoresCompletedEventArgs e)
        {
            var list = e.Result;

            Dispatcher.BeginInvoke((Action)delegate
            {
                HighscoreList.Clear();

                if (list == null) return;

                foreach (var item in list)
                {
                    HighscoreList.Add(item);
                }
            });
        }
    }
}
